import pymysql


class MySqlHelper:
    def __init__(self, host, user, password, database, port=3306, charset='utf8'):
        self.host = host
        self.user = user
        self.password = password
        self.database = database
        self.port = port
        self.charset = charset
        self.connection = None
        self.cursor = None

    def connect(self):
        self.connection = pymysql.Connection(
            host=self.host,
            user=self.user,
            password=self.password,
            database=self.database,
            port=self.port,
            charset=self.charset
        )
        self.cursor = self.connection.cursor()

    def close(self):
        self.cursor.close()
        self.connection.close()

    def insert(self, sql, params=[]):
        count = None
        try:
            self.connect()
            count = self.cursor.execute(sql, params)
            self.connection.commit()
            self.close()
        except Exception as e:
            print(e)
        return count

    def fetch_one(self, sql, params=[]):
        count = None
        try:
            self.connect()
            count = self.cursor.execute(sql, params)
            self.cursor.fetchone()
            self.close()
        except Exception as e:
            print(e)
        return count


class Person:
    def __init__(self, username, password):
        self.username = username
        self.password = password

    def login(self):
        sql = "select * from tb_users where username=%s and password=%s"
        params = [self.username, self.password]
        helper = MySqlHelper('localhost', 'root', 'ak47qbz95', 'hqyj')
        count = helper.fetch_one(sql, params)
        if count > 0:
            print("登录成功")
            return True
        else:
            print("登录失败")

    def register(self):
        sql = "insert into tb_users(username,password) values(%s,%s)"
        params = [self.username, self.password]
        helper = MySqlHelper('localhost', 'root', 'ak47qbz95', 'hqyj')
        try:
            count = helper.insert(sql, params)
            if count > 0:
                print("注册成功")
            else:
                print("注册失败")
        except Exception as e:
            print(e)
            print("用户名已存在!")

    def change_password(self, new_password):
        if self.login():
            sql = "update tb_users set password=%s where username=%s"
            params = [new_password, self.username]
            helper = MySqlHelper('localhost', 'root', 'ak47qbz95', 'hqyj')
            count = helper.insert(sql, params)
            self.password = new_password
            if count > 0:
                print("修改密码成功")
            else:
                print("修改密码失败")

    def cancel(self):
        if self.login():
            sql = "delete from tb_users where username=%s"
            params = [self.username]
            helper = MySqlHelper('localhost', 'root', 'ak47qbz95', 'hqyj')
            count = helper.insert(sql, params)
            if count > 0:
                print("注销成功")
            else:
                print("注销失败")


if __name__ == '__main__':
    while True:
        print("欢迎来到淘宝")
        print("1.登录")
        print("2.注册")
        print("0.退出")
        k = input("请选择：")
        if k == '1':
            username = input("输入用户名：")
            password = input("输入密码：")
            person = Person(username, password)
            if person.login():
                while True:
                    print("个人中心")
                    print("1.修改密码")
                    print("2.注销账号")
                    print("0.登出")
                    k = input("请选择：")
                    if k == '1':
                        print("修改密码")
                        password = input("输入原密码：")
                        new_password = input("输入新密码：")
                        if person.password == password:
                            person.change_password(new_password)
                        else:
                            print("原密码错误")
                            continue
                    elif k == '2':
                        t = input("确认注销注销账号吗(y/n):")
                        if t == 'y':
                            person.cancel()
                            break
                        else:
                            continue
                    elif k == '0':
                        break
        elif k == '2':
            username = input("输入用户名：")
            password = input("输入密码：")
            person = Person(username, password)
            person.register()
        elif k == '0':
            break
